/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.j2eem.Aop;

import com.mycompany.j2eem.Entity.JsonResult;
import com.mycompany.j2eem.Entity.ResultCode;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 *
 * @author asus
 */
@Aspect
@Component
public class IP {

    @Pointcut("@annotation(com.mycompany.j2eem.Aop.GetIp)")
    public void VisitIp() {
    }

    @Before("VisitIp()")
    public Object doBefore(JoinPoint joinPoint) throws Throwable {
        System.out.println("IP");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
             System.out.println( request.getRemoteAddr());
            return request.getRemoteAddr();
        }
         System.out.println(request.getHeader("x-forwarded-for"));
        return request.getHeader("x-forwarded-for");
    }
}
